package com.xuelang.pipeline.worker.app.common.util;

import com.xuelang.pipeline.worker.client.common.exception.BizException;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.util.Config;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @author liuyuan.ly
 * @since 2024/5/16 18:18
 */

@Slf4j
public class K8sUtils {

    public static ApiClient getApiClientByStr(String Str) {
        InputStream inputStream = new ByteArrayInputStream(Str.getBytes());
        ApiClient client = null;
        try {
            client = Config.fromConfig(inputStream).setVerifyingSsl(false);
        } catch (Exception e) {
            log.error("加载kubeConfig 失败，key={}", inputStream, e);
            throw new BizException("配置集群连接异常，请检查多云集成中所在KubeConfig是否准确。");
        }
        return client;
    }

    public static void initApiClient(String kubeConfig){
        ApiClient apiClient = getApiClientByStr(kubeConfig);
        Configuration.setDefaultApiClient(apiClient);
    }

}
